{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#加载飞桨和相关类库\n",
    "import paddle\n",
    "import paddle.fluid as fluid\n",
    "from paddle.fluid.dygraph.nn import Linear\n",
    "import numpy as np\n",
    "import os\n",
    "from PIL import Image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/mnt/data8/zhangyiming/.env/pyenv/versions/3.8.1/lib/python3.8/site-packages/ipykernel/ipkernel.py:287: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above.\n",
      "  and should_run_async(code)\n"
     ]
    }
   ],
   "source": [
    "# 如果～/.cache/paddle/dataset/mnist/目录下没有MNIST数据，API会自动将MINST数据下载到该文件夹下\n",
    "# 设置数据读取器，读取MNIST数据训练集\n",
    "trainset = paddle.dataset.mnist.train()\n",
    "# 包装数据读取器，每次读取的数据数量设置为batch_size=8\n",
    "train_reader = paddle.batch(trainset, batch_size=8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1., -1., -1., ..., -1., -1., -1.],\n",
       "       [-1., -1., -1., ..., -1., -1., -1.],\n",
       "       [-1., -1., -1., ..., -1., -1., -1.],\n",
       "       ...,\n",
       "       [-1., -1., -1., ..., -1., -1., -1.],\n",
       "       [-1., -1., -1., ..., -1., -1., -1.],\n",
       "       [-1., -1., -1., ..., -1., -1., -1.]], dtype=float32)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([x[0] for x in data])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "图像数据形状和对应数据为: (8, 784) [-1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -0.9764706  -0.85882354 -0.85882354 -0.85882354\n",
      " -0.01176471  0.06666672  0.37254906 -0.79607844  0.30196083  1.\n",
      "  0.9372549  -0.00392157 -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -0.7647059  -0.7176471  -0.26274508  0.20784318\n",
      "  0.33333337  0.9843137   0.9843137   0.9843137   0.9843137   0.9843137\n",
      "  0.7647059   0.34901965  0.9843137   0.8980392   0.5294118  -0.4980392\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -0.6156863\n",
      "  0.8666667   0.9843137   0.9843137   0.9843137   0.9843137   0.9843137\n",
      "  0.9843137   0.9843137   0.9843137   0.96862745 -0.27058822 -0.35686272\n",
      " -0.35686272 -0.56078434 -0.69411767 -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -0.85882354  0.7176471   0.9843137\n",
      "  0.9843137   0.9843137   0.9843137   0.9843137   0.5529412   0.427451\n",
      "  0.9372549   0.8901961  -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -0.372549    0.22352946 -0.1607843   0.9843137\n",
      "  0.9843137   0.60784316 -0.9137255  -1.         -0.6627451   0.20784318\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -0.8901961  -0.99215686  0.20784318  0.9843137  -0.29411763\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.          0.09019613  0.9843137   0.4901961  -0.9843137  -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -0.9137255\n",
      "  0.4901961   0.9843137  -0.45098037 -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -0.7254902   0.8901961\n",
      "  0.7647059   0.254902   -0.15294117 -0.99215686 -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -0.36470586  0.88235295  0.9843137\n",
      "  0.9843137  -0.06666666 -0.8039216  -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -0.64705884  0.45882356  0.9843137   0.9843137\n",
      "  0.17647064 -0.7882353  -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -0.8745098  -0.27058822  0.9764706   0.9843137   0.4666667\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.          0.9529412   0.9843137   0.9529412  -0.4980392  -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -0.6392157   0.0196079   0.43529415  0.9843137\n",
      "  0.9843137   0.62352943 -0.9843137  -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -0.69411767  0.16078436\n",
      "  0.79607844  0.9843137   0.9843137   0.9843137   0.9607843   0.427451\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -0.8117647  -0.10588235  0.73333335  0.9843137   0.9843137   0.9843137\n",
      "  0.9843137   0.5764706  -0.38823527 -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -0.81960785 -0.4823529   0.67058825  0.9843137\n",
      "  0.9843137   0.9843137   0.9843137   0.5529412  -0.36470586 -0.9843137\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -0.85882354  0.3411765\n",
      "  0.7176471   0.9843137   0.9843137   0.9843137   0.9843137   0.5294118\n",
      " -0.372549   -0.92941177 -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -0.5686275   0.34901965  0.77254903  0.9843137   0.9843137   0.9843137\n",
      "  0.9843137   0.9137255   0.04313731 -0.9137255  -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.          0.06666672  0.9843137\n",
      "  0.9843137   0.9843137   0.6627451   0.05882359  0.03529418 -0.8745098\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.         -1.         -1.\n",
      " -1.         -1.         -1.         -1.        ]\n",
      "图像标签形状和对应数据为: (8,) 5.0\n",
      "\n",
      "打印第一个batch的第一个图像，对应标签数字为5.0\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAEICAYAAACZA4KlAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/d3fzzAAAACXBIWXMAAAsTAAALEwEAmpwYAAAQaUlEQVR4nO3de4xc5X3G8e+DrzEYanNxjXHBAXMNwaQrLoUSqgQXUCJAlZy4UeoQqCGFFARKAVcNFkIVVATiIKA1xWBSMJcSgiORC7EQJGpwWYixDS5gsAk2yxrjgIEQs17/+sccowF23lnP3fs+H2m1Z87vnDm/HXh8ZuY9M68iAjMb+nZpdwNm1hoOu1kmHHazTDjsZplw2M0y4bCbZcJhH6IkPSvp5Hb3YZ1DHmc3y4PP7GaZcNiHKElrJX1R0lxJ90v6L0nvSFoh6WBJV0jaIOlVSdPL9jtb0qpi25clnfex+/0nST2SXpN0rqSQdFBRGyXpOkm/k9Qr6d8lfarVf7sNzGHPw5eBHwLjgN8CP6f0334ScBXwH2XbbgC+BOwOnA3cIOlzAJJOBS4BvggcBJz8seNcAxwMTCvqk4DvNuHvsRr4NfsQJWktcC5wInBCRJxSrP8ysAjYIyL6JY0FNgPjIuKtAe7nx8CjETFP0gKgNyKuKGoHAS8CU4GXgHeBz0bES0X9eODuiJjSzL/VBmd4uxuwlugtW34f2BgR/WW3AXYD3pJ0GnAlpTP0LsAYYEWxzb5Ad9l9vVq2vHex7VOStq8TMKxBf4PVyWG3D0kaBTwA/B3wUET0FWf27entAfYr22Vy2fJGSv9wHBER61vQru0gv2a3ciOBUcAbwNbiLD+9rH4fcLakwySNAf5ley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yjHRcRlHdLbXODddk/jXcxWNLF8mnHgTOAbtPGxS/Q1gxY8bu04sx8DrI6IlyPiA+Ae4Iw29NHxIuJxYNPHVp8BLCyWF1L6n6XlKvTWESKiJyKeLpbfAbZPM97Wxy7RV0u0I+yTgFfLbq+js+Z7D+AXkp6SNLvdzQxgQkT0FMuvAxPa2cwAqk7j3Uofm2a8Yx67WqY/r5ffoPukEyPic8BpwAXF09WOFKXXYJ00djqoabxbZYBpxj/Uzseu1unP69WOsK8HJpfd3q9Y1xEiYn3xewPwIJ03FXXv9hl0i98b2tzPhzppGu+BphmnAx67dk5/3o6wPwlMlTRF0kjgq8DiNvTxCZJ2Ld44QdKuwHQ6byrqxcCsYnkW8FAbe/mITpnGu9I047T5sWv79OcR0fIf4HRK78i/BPxzO3qo0NengWeKn2fb3RuwiNLTuj5K722cA+wJLAFeBH4JjO+g3n4IrACWUwrWxDb1diKlp+jLgWXFz+ntfuwSfbXkcfPlsmaZ8Bt0Zplw2M0y4bCbZcJhN8uEw26WCYfdLBMOu1km/h849QkflMaTDgAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 以迭代的形式读取数据\n",
    "for batch_id, data in enumerate(train_reader()):\n",
    "    # 获得图像数据，并转为float32类型的数组\n",
    "    img_data = np.array([x[0] for x in data]).astype('float32')\n",
    "    # 获得图像标签数据，并转为float32类型的数组\n",
    "    label_data = np.array([x[1] for x in data]).astype('float32')\n",
    "    # 打印数据形状\n",
    "    print(\"图像数据形状和对应数据为:\", img_data.shape, img_data[0])\n",
    "    print(\"图像标签形状和对应数据为:\", label_data.shape, label_data[0])\n",
    "    break\n",
    "\n",
    "print(\"\\n打印第一个batch的第一个图像，对应标签数字为{}\".format(label_data[0]))\n",
    "# 显示第一batch的第一个图像\n",
    "import matplotlib.pyplot as plt\n",
    "img = np.array(img_data[0]+1)*127.5\n",
    "img = np.reshape(img, [28, 28]).astype(np.uint8)\n",
    "\n",
    "plt.figure(\"Image\") # 图像窗口名称\n",
    "plt.imshow(img)\n",
    "plt.axis('on') # 关掉坐标轴为 off\n",
    "plt.title('image') # 图像题目\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/mnt/data8/zhangyiming/.env/pyenv/versions/3.8.1/lib/python3.8/site-packages/paddle/fluid/dygraph/tracer.py:43: DeprecationWarning: an integer is required (got type paddle.fluid.core_avx.VarType).  Implicit conversion to integers using __int__ is deprecated, and may be removed in a future version of Python.\n",
      "  self.trace(type, inputs, outputs, attrs,\n"
     ]
    }
   ],
   "source": [
    "# 定义mnist数据识别网络结构，同房价预测网络\n",
    "class MNIST(fluid.dygraph.Layer):\n",
    "    def __init__(self):\n",
    "        super(MNIST, self).__init__()\n",
    "        \n",
    "        # 定义一层全连接层，输出维度是1，激活函数为None，即不使用激活函数\n",
    "        self.fc = Linear(input_dim=784, output_dim=1, act=None)\n",
    "        \n",
    "    # 定义网络结构的前向计算过程\n",
    "    def forward(self, inputs):\n",
    "        outputs = self.fc(inputs)\n",
    "        return outputs\n",
    "\n",
    "\n",
    "# 定义飞桨动态图工作环境\n",
    "with fluid.dygraph.guard():\n",
    "    # 声明网络结构\n",
    "    model = MNIST()\n",
    "    # 启动训练模式\n",
    "    model.train()\n",
    "    # 定义数据读取函数，数据读取batch_size设置为16\n",
    "    train_loader = paddle.batch(paddle.dataset.mnist.train(), batch_size=16)\n",
    "    # 定义优化器，使用随机梯度下降SGD优化器，学习率设置为0.001\n",
    "    optimizer = fluid.optimizer.SGDOptimizer(\n",
    "        learning_rate=0.001, \n",
    "        parameter_list=model.parameters()\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch: 0, batch: 1000, loss is: [2.288066]\n",
      "epoch: 0, batch: 2000, loss is: [3.9089444]\n",
      "epoch: 0, batch: 3000, loss is: [3.729089]\n",
      "epoch: 1, batch: 1000, loss is: [2.0253582]\n",
      "epoch: 1, batch: 2000, loss is: [3.8309412]\n",
      "epoch: 1, batch: 3000, loss is: [3.5790591]\n",
      "epoch: 2, batch: 1000, loss is: [1.9423126]\n",
      "epoch: 2, batch: 2000, loss is: [3.7585502]\n",
      "epoch: 2, batch: 3000, loss is: [3.4692533]\n",
      "epoch: 3, batch: 1000, loss is: [1.9098004]\n",
      "epoch: 3, batch: 2000, loss is: [3.7201018]\n",
      "epoch: 3, batch: 3000, loss is: [3.3819036]\n",
      "epoch: 4, batch: 1000, loss is: [1.8974979]\n",
      "epoch: 4, batch: 2000, loss is: [3.703333]\n",
      "epoch: 4, batch: 3000, loss is: [3.3096428]\n",
      "epoch: 5, batch: 1000, loss is: [1.8941026]\n",
      "epoch: 5, batch: 2000, loss is: [3.6987448]\n",
      "epoch: 5, batch: 3000, loss is: [3.2490413]\n",
      "epoch: 6, batch: 1000, loss is: [1.8950456]\n",
      "epoch: 6, batch: 2000, loss is: [3.7007642]\n",
      "epoch: 6, batch: 3000, loss is: [3.1979444]\n",
      "epoch: 7, batch: 1000, loss is: [1.8982427]\n",
      "epoch: 7, batch: 2000, loss is: [3.7063043]\n",
      "epoch: 7, batch: 3000, loss is: [3.1547365]\n",
      "epoch: 8, batch: 1000, loss is: [1.9026284]\n",
      "epoch: 8, batch: 2000, loss is: [3.7136502]\n",
      "epoch: 8, batch: 3000, loss is: [3.1181068]\n",
      "epoch: 9, batch: 1000, loss is: [1.907614]\n",
      "epoch: 9, batch: 2000, loss is: [3.7218208]\n",
      "epoch: 9, batch: 3000, loss is: [3.0869722]\n"
     ]
    }
   ],
   "source": [
    "# 通过with语句创建一个dygraph运行的context\n",
    "# 动态图下的一些操作需要在guard下进行\n",
    "with fluid.dygraph.guard():\n",
    "    model = MNIST()\n",
    "    model.train()\n",
    "    train_loader = paddle.batch(paddle.dataset.mnist.train(), batch_size=16)\n",
    "    optimizer = fluid.optimizer.SGDOptimizer(learning_rate=0.001, parameter_list=model.parameters())\n",
    "    EPOCH_NUM = 10\n",
    "    for epoch_id in range(EPOCH_NUM):\n",
    "        for batch_id, data in enumerate(train_loader()):\n",
    "            #准备数据，格式需要转换成符合框架要求\n",
    "            image_data = np.array([x[0] for x in data]).astype('float32')\n",
    "            label_data = np.array([x[1] for x in data]).astype('float32').reshape(-1, 1)\n",
    "            # 将数据转为飞桨动态图格式\n",
    "            image = fluid.dygraph.to_variable(image_data)\n",
    "            label = fluid.dygraph.to_variable(label_data)\n",
    "            \n",
    "            #前向计算的过程\n",
    "            predict = model(image)\n",
    "            \n",
    "            #计算损失，取一个批次样本损失的平均值\n",
    "            loss = fluid.layers.square_error_cost(predict, label)\n",
    "            avg_loss = fluid.layers.mean(loss)\n",
    "            \n",
    "            #每训练了1000批次的数据，打印下当前Loss的情况\n",
    "            if batch_id !=0 and batch_id  % 1000 == 0:\n",
    "                print(\"epoch: {}, batch: {}, loss is: {}\".format(epoch_id, batch_id, avg_loss.numpy()))\n",
    "            \n",
    "            #后向传播，更新参数的过程\n",
    "            avg_loss.backward()\n",
    "            optimizer.minimize(avg_loss)\n",
    "            model.clear_gradients()\n",
    "\n",
    "    # 保存模型\n",
    "    fluid.save_dygraph(model.state_dict(), 'mnist')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(16, 784)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "image_data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(16, 1)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "label_data.shape"
   ]
  },
  {
   "cell_type": "code",
   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enVWZ/3qr/Gew9JD9t+yvZ408XMYTgi9hXT+yUNN1nMHNoO491L7xhmvG/2XSfDn1fFBbp3uyIiflvSJyV9tjhd7Usx8xusn/pO5zWMd6/MMcz4rzS57zod/ryqJsI+JWn1rM/nF/P6QkRMFe/Tku5X/w1FfeCtEXSL9+mG6/mVfhrGe65hxtUH+67J4c+bCPuTki6yfYHtxZI+JWlbA3W8i+3lxYUT2V4u6Sr131DU2yRtLKY3SnqgwVrepl+G8W41zLga3neND38eET1/SbpGM1fkfyrpL5qooUVdH5L0n8XruaZrk3SvZk7rjmvm2sbNks6VtEPSi5IekTTUR7X9k6RnJT2jmWCNNFTbFZo5RX9G0s7idU3T+66krp7sN26XBZLgAh2QBGEHkiDsQBKEHUiCsANJEHYgCcIOJPH/iuLyTppMcnAAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(28, 28)\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/d3fzzAAAACXBIWXMAAAsTAAALEwEAmpwYAAANQ0lEQVR4nO3db4wc9X3H8c/Hx9mOTVB8EF8vxo0JgUi0Ui/VYdpCUlc0iKAig0pRLAW5EurlQSwFKQ+gtFWo8qAkaoJQGyFdajdOlYJSJRRXIgFjoSLUhHAQFwwmhVATfLV9Tp3KJoD/fvvghuiA29nzzuzO+r7vl7Ta2fnN7Hw18sczO7+Z+zkiBGDhW9R0AQB6g7ADSRB2IAnCDiRB2IEkzurlxhZ7SSzV8l5uEkjlTf1Sx+Ko52qrFHbbV0u6W9KApH+IiDvLll+q5brMV1bZJIAST8SOlm0dn8bbHpD0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gJrM52r845Lm6rcr7VMH0F+4gw5IgrADSRB2IAnCDiRB2IEkCDuQBGEHkiDsQBKEHUiCsANJEHYgCcIOJEHYgSQIO5BE2z8lXevG7IOSXpk16zxJP+9ZAaenX2vr17okautUnbV9MCLmHAu7p2F/18btyYgYa6yAEv1aW7/WJVFbp3pVG6fxQBKEHUii6bBPNLz9Mv1aW7/WJVFbp3pSW6O/2QH0TtNHdgA9QtiBJBoJu+2rbf/E9ku2b2uihlZs77H9rO2dticbrmWL7Wnbu2bNG7K93faLxfucY+w1VNsdtqeKfbfT9jUN1bba9qO2n7f9nO3PFfMb3XcldfVkv/X8N7vtAUn/JekTkvZKelLShoh4vqeFtGB7j6SxiGj8BgzbH5f0mqRvRsRvFvO+LOlQRNxZ/Ee5IiJu7ZPa7pD0WtPDeBejFY3MHmZc0nWS/lQN7ruSum5UD/ZbE0f2tZJeioiXI+KYpPskrW+gjr4XEY9JOvSO2eslbS2mt2rmH0vPtaitL0TEvoh4upg+IumtYcYb3XcldfVEE2FfJenVWZ/3qr/Gew9JD9t+yvZ408XMYTgi9hXT+yUNN1nMHNoO491L7xhmvG/2XSfDn1fFBbp3uyIiflvSJyV9tjhd7Usx8xusn/pO5zWMd6/MMcz4rzS57zod/ryqJsI+JWn1rM/nF/P6QkRMFe/Tku5X/w1FfeCtEXSL9+mG6/mVfhrGe65hxtUH+67J4c+bCPuTki6yfYHtxZI+JWlbA3W8i+3lxYUT2V4u6Sr131DU2yRtLKY3SnqgwVrepl+G8W41zLga3neND38eET1/SbpGM1fkfyrpL5qooUVdH5L0n8XruaZrk3SvZk7rjmvm2sbNks6VtEPSi5IekTTUR7X9k6RnJT2jmWCNNFTbFZo5RX9G0s7idU3T+66krp7sN26XBZLgAh2QBGEHkiDsQBKEHUiCsANJEHYgCcIOJPH/iuLyTppMcnAAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(28, 28)\n"
     ]
    }
   ],
   "source": [
    "# 导入图像读取第三方库\n",
    "import matplotlib.image as mpimg\n",
    "import matplotlib.pyplot as plt\n",
    "import cv2\n",
    "import numpy as np\n",
    "\n",
    "\n",
    "# 读取图像\n",
    "img1 = np.array(img_data[2]+1)*127.5\n",
    "img1 = np.reshape(img1, [28, 28]).astype(np.uint8)\n",
    "\n",
    "# 显示图像\n",
    "plt.imshow(img1)\n",
    "plt.show()\n",
    "\n",
    "print(img1.shape)\n",
    "\n",
    "im = Image.fromarray(img1)\n",
    "im = im.convert('L').resize((28, 28), Image.ANTIALIAS)\n",
    "plt.imshow(im)\n",
    "plt.show()\n",
    "print(np.array(im).shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
